package com.magesti.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.magesti.DAO.DaoFactory;
import com.magesti.DAOinterface.CalendrierInterfaceDAO;
import com.magesti.DAOinterface.HistoriqueInterfaceDAO;
import com.magesti.DAOinterface.OperationInterfaceDAO;
import com.magesti.beans.Calendrier;
import com.magesti.beans.Operation;
import com.magesti.exception.DAOException;
import com.magesti.form.ListePhaseForm;
import com.magesti.ressource.Global;
import com.magesti.ressource.ServletGlobal;

//Cette servlet sert de prototye pour creer de nouveaux servlet !
@SuppressWarnings("serial")
public class ListePhase extends HttpServlet {

	private CalendrierInterfaceDAO calendrierDAO;
	private OperationInterfaceDAO operationDAO;
	private HistoriqueInterfaceDAO historiqueDAO;

	public void init() throws ServletException {
		DaoFactory f = ((DaoFactory) getServletContext().getAttribute(
				Global.ATT_CONTEXT_DAO_FACTORY));
		calendrierDAO = f.getCalendrierDAO();
		operationDAO = f.getOperationDAO();
		historiqueDAO = f.getHistoriqueDAO();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	
		int id_op = Global.VALEUR_ID_DEFAUT;
		Operation op = null;
		Calendrier calendrier = null;

		ListePhaseForm form = new ListePhaseForm(calendrierDAO,historiqueDAO);

		try {
			id_op = ServletGlobal.checkIdOperation(request);
			op = operationDAO.createOperation(id_op);
			
			calendrier = form.createCalendrier( id_op);
			calendrier = form.createDefautCalendrier(calendrier);

			op.setCalendrier(calendrier);

		} catch (DAOException e) {
			e.printStackTrace();
		}

		request.setAttribute(Global.ATT_REQUEST_OPERATION, op);
		request.setAttribute(Global.ATT_REQUEST_CALENDRIER, calendrier);
		request.setAttribute(Global.ATT_REQUEST_FORM, form);
		ServletGlobal.setRightsUser(request);
		
		this.getServletContext()
				.getRequestDispatcher(Global.VUE_JSP_LISTE_PHASE)
				.forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
			
		int id_op = ServletGlobal.checkIdOperation(request);
		Operation op=null;
		boolean estPrevisionnel = false;
		
		try {
			op = operationDAO.createOperation(id_op);
			
		} catch (DAOException e) {
			e.printStackTrace();
		}

		estPrevisionnel = op.getLibelle().estPrevisionnel();
		ListePhaseForm form = new ListePhaseForm(calendrierDAO,historiqueDAO);
		Calendrier calendrier = form.createCalendrier(request,id_op,estPrevisionnel);
		//calendrier = form.createDefautCalendrier(calendrier);

		//TODO supprimer se set une fois que le vrai DAO sera en place.
		op.setCalendrier(calendrier);
		
		request.setAttribute(Global.ATT_REQUEST_OPERATION, op);
		request.setAttribute(Global.ATT_REQUEST_CALENDRIER, calendrier);
		request.setAttribute(Global.ATT_REQUEST_FORM, form);
		ServletGlobal.setRightsUser(request);
		
		this.getServletContext()
				.getRequestDispatcher(Global.VUE_JSP_LISTE_PHASE)
				.forward(request, response);
	}

}
